﻿using UnityEngine;
using System.Collections;

public class Calc : MonoBehaviour {
	
	string val;
	int temp;
	Rect text,button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,buttonPlus,buttonEqual,buttonClear;
	bool endOfCalc;
	void Start () {
		text = new Rect (10, 10, 200, 20);
		button1 = new Rect (10, 30, 20, 20);
		button2 = new Rect (30, 30, 20, 20);
		button3 = new Rect(50,30,20,20);
		button4 = new Rect (70, 30, 20, 20);
		button5 = new Rect (90, 30, 20, 20);
		button6 = new Rect (110, 30, 20, 20);
		button7 = new Rect (130, 30, 20, 20);
		button8 = new Rect (150, 30, 20, 20);
		button9 = new Rect (170, 30, 20, 20);
		button0 = new Rect (190, 30, 20, 20);
		buttonPlus = new Rect (200, 50, 20, 20);
		buttonEqual = new Rect (220, 50, 20, 20);
		buttonClear = new Rect (240, 50, 20, 20);
		val ="";
		endOfCalc = false;
	}
	
	// Update is called once per frame
	void OnGUI () {
		GUI.TextField (text, val);
		if(GUI.Button (button1,"1")){
			Calculation("1");
		}
		if(GUI.Button (button2,"2")){
			Calculation("2");
		}
		if(GUI.Button (button3,"3")){
			Calculation("3");
		}
		if(GUI.Button (button4,"4")){
			Calculation("4");
		}
		if(GUI.Button (button5,"5")){
			Calculation("5");
		}
		if(GUI.Button (button6,"6")){
			Calculation("6");
		}
		if(GUI.Button (button7,"7")){
			Calculation("7");
		}
		if(GUI.Button (button8,"8")){
			Calculation("8");
		}
		if(GUI.Button (button9,"9")){
			Calculation("9");
		}
		if (GUI.Button (button0, "0")) {
			Calculation("0");
		}
		if(GUI.Button (buttonPlus,"+")){
			temp +=int.Parse(val);
			val = "";
		}
		if(GUI.Button (buttonEqual,"=")){
			temp +=int.Parse(val);
			val = temp.ToString();
			endOfCalc = true;
		}
		if (GUI.Button (buttonClear, "Clear")) {
			val = "";
		}
	}
	void Calculation(string str){
		if(!endOfCalc)
			val += str;
		else{
			val = "";
			val += str;
			endOfCalc = false;
			temp = 0;
		}
	}
}
